package com.dlu.safe_construction.service.impl;

import com.dlu.safe_construction.common.ApiResult;
import com.dlu.safe_construction.common.CommonEnum;
import com.dlu.safe_construction.common.utils.Utils;
import com.dlu.safe_construction.mapper.OwnerMapper;
import com.dlu.safe_construction.model.Owner;
import com.dlu.safe_construction.service.OwnerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by  lijiaxing on 2022/4/3 16:17
 *
 * @author lijiaxing
 */
@Service
@Slf4j
public class OwnerServiceImpl implements OwnerService {

    @Resource
    private OwnerMapper ownerMapper;

    //TODO 完善代码逻辑以及错误码编写记录
    @Override
    public ApiResult<Void> add(Owner owner) {
        owner.setOwnerId(Utils.generateId());
        log.error(owner.getOwnerId());
        int ret = ownerMapper.insert(owner);
        return ApiResult.success();
    }

    @Override
    public ApiResult<Void> deleteBy(String key, Integer byWhat) {
        if (CommonEnum.OwnerDeleteBy.DELETE_BY_OWNER_ID.isThisStatus(byWhat)) {
            int ret = ownerMapper.deleteByPrimaryKey(key);
            return ApiResult.success();
        } else if (CommonEnum.OwnerDeleteBy.DELETE_BY_OWNER_PHONE.isThisStatus(byWhat)) {
            int ret = ownerMapper.deleteByOwnerPhone(key);
            return ApiResult.success();
        }
        return ApiResult.fail(8000);
    }

    @Override
    public ApiResult<Void> edit(Owner owner) {
        int ret = ownerMapper.updateByPrimaryKeySelective(owner);
        return ApiResult.success();
    }

    @Override
    public ApiResult<List<Owner>> list() {
        List<Owner> owners = ownerMapper.selectAll();
        return ApiResult.success(owners);
    }

    @Override
    public ApiResult<List<Owner>> searchBy(String key, Integer byWhat) {
        List<Owner> owners = null;
        if (CommonEnum.OwnerSearchBy.SEARCH_BY_OWNER_ID.isThisStatus(byWhat)) {
            owners = new ArrayList<>();
            owners.add(ownerMapper.selectByPrimaryKey(key));
            return ApiResult.success(owners);
        } else if (CommonEnum.OwnerSearchBy.SEARCH_BY_OWNER_PHONE.isThisStatus(byWhat)) {
            owners = ownerMapper.selectByOwnerPhone(key);
            return ApiResult.success(owners);
        } else if (CommonEnum.OwnerSearchBy.SEARCH_BY_OWNER_NAME.isThisStatus(byWhat)) {
            owners = ownerMapper.selectByOwnerName(key);
            return ApiResult.success(owners);
        } else if (CommonEnum.OwnerSearchBy.SEARCH_BY_OWNER_STATUS.isThisStatus(byWhat)) {
            owners = ownerMapper.selectByOwnerStatus(Integer.valueOf(key));
            return ApiResult.success(owners);
        }
        return ApiResult.fail(8000);
    }
}
